<?php

namespace app\common\model;
use think\Model;

class GoodsComment extends Model
{
	protected $table = 'goods_comment';
	protected $pk = 'comment_id';
	protected $autoWriteTimestamp = true;
	protected $createTime = 'comment_create_time'; 
	protected $updateTime = false;

    //获取数据
	public static function showlist($pagesize)
	{
		// 已回复评论
		$data['replyed'] = GoodsComment::field('a.comment_id,a.comment_content,a.comment_user_id,a.comment_goods_id,a.comment_create_time,
		b.replay_reply,a.comment_is_reply,c.nickname,d.goods_name')->alias('a')
		->join('goods_comment_replay b', 'a.comment_id=b.comment_id', 'left')
		->join('user c', 'a.comment_user_id=c.user_id', 'left')
		->join('goods d', 'a.comment_goods_id=d.goods_id', 'left')
		// ->join('merchant f', 'd.goods_merchant_id=f.merchant_id')
		->where([
			'a.comment_is_reply' => 2,
			'a.comment_is_delete' => 0,
			'd.goods_merchant_id' => session('merchant.merchant_id')
			// 'a.'
		])->order('a.comment_id', 'desc')->paginate($pagesize);

		// 未回复评论
		$data['not_reply'] = GoodsComment::field('a.comment_id,a.comment_content,a.comment_user_id,a.comment_goods_id,a.comment_create_time,
		b.replay_reply,a.comment_is_reply,c.nickname,d.goods_name')->alias('a')
		->join('goods_comment_replay b', 'a.comment_id=b.comment_id', 'left')
		->join('user c', 'a.comment_user_id=c.user_id', 'left')
		->join('goods d', 'a.comment_goods_id=d.goods_id', 'left')
		// ->join('merchant f', 'd.goods_merchant_id=f.merchant_id')
		->where([
			'a.comment_is_reply' => 1,
			'a.comment_is_delete' => 0,
			'd.goods_merchant_id' => session('merchant.merchant_id')
		])->order('a.comment_id ', 'desc')->paginate($pagesize);
		return $data;
	}

    //获取图书评论数据
    public static function showbooklist($pagesize)
    {
        // 已回复评论
        $data['replyed'] = GoodsComment::field('a.comment_id,a.comment_content,a.comment_user_id,a.comment_goods_id,a.comment_create_time,
		b.replay_reply,a.comment_is_reply,c.nickname,d.title')->alias('a')
            ->join('goods_comment_replay b', 'a.comment_id=b.comment_id', 'left')
            ->join('user c', 'a.comment_user_id=c.user_id', 'left')
            ->join('zj_book d', 'a.book_id=d.id', 'left')
            // ->join('merchant f', 'd.goods_merchant_id=f.merchant_id')
            ->where([
                'a.comment_is_reply' => 2,
                'a.comment_is_delete' => 0,
                'd.merchant_id' => session('merchant.merchant_id')
                // 'a.'
            ])->order('a.comment_id', 'desc')->paginate($pagesize);

        // 未回复评论
        $data['not_reply'] = GoodsComment::field('a.comment_id,a.comment_content,a.comment_user_id,a.comment_goods_id,a.comment_create_time,
		b.replay_reply,a.comment_is_reply,c.nickname,d.title')->alias('a')
            ->join('goods_comment_replay b', 'a.comment_id=b.comment_id', 'left')
            ->join('user c', 'a.comment_user_id=c.user_id', 'left')
            ->join('zj_book d', 'a.book_id=d.id', 'left')
            // ->join('merchant f', 'd.goods_merchant_id=f.merchant_id')
            ->where([
                'a.comment_is_reply' => 1,
                'a.comment_is_delete' => 0,
                'd.merchant_id' => session('merchant.merchant_id')
            ])->order('a.comment_id ', 'desc')->paginate($pagesize);
        return $data;
    }


	// 建立关联模型
	public function reply()
	{
		return $this->hasOne('GoodsReply', 'reply_comment_id', 'comment_id')->field('reply_content');
	}
    public static function insert($data)
	{
		$result = db('goods_reply')->insert($data);
		 
        if($result)
		{
			$r = GoodsComment::where('comment_id', $data['reply_comment_id'])->setField('comment_is_reply', 2);
		}
		return $result;
	}

	public static function insertGoodsCommentReplay($data)
	{
		$result = db('goods_comment_replay')->insert($data);

		if($result)
		{
			$r = GoodsComment::where('comment_id', $data['comment_id'])->setField('comment_is_reply', 2);
		}
		return $result;
	}

	// 用户属性获取器
	// public function getCommentUserIdAttr($value)
	// {
	// 	$value = db('user')->field('username')->where('user_id', $value)->find();
	// 	$value = $value['username'];
	// 	return $value;
	// } 


	// 商品名字获取器
	public function getCommentGoodsIdAttr($value)
	{
		$value = db('goods')->field('goods_name')->where('goods_id', $value)->find();
		$value = $value['goods_name'];
		return $value;
	}  

}